<template>
	<view class="container-conversation">
		<view class="scroll-box">
			<view class="empty_box" v-if="conversationList.length == 0">
				<image src="@/static/index/emptyBox.png"></image>
				<text>什么都没有</text>
			</view>
			<view class="uni-list margintop-bar">
				<view v-for="item in conversationList" :key="item.conversationID">
					<TUI-conversation-item :data-type="item.type" :conversation="item"></TUI-conversation-item>
				</view>
			</view>
		</view>
	</view>
</template>
<!-- 这里有个加载的补丁逻辑待优化，页面向SDK请求conversationList的时候会有时延，
造成页面的一个抖动，这里加一个if逻辑打一个补丁，后续继续优化 -->
<script>
	import logger from '@/subpkg/tim/utils/logger';
	import TUIConversationItem from '@/subpkg/tim/components/tui-conversation/conversation-item/index';
	import TUIMessageList from '@/subpkg/tim/components/tui-chat/message-list/index';

	export default {
		data() {
			return {
				conversationList: [],
			};
		},

		components: {
			TUIConversationItem,
			TUIMessageList
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad() {
			// 登入后拉去会话列表
			this.getConversationList();
			uni.$TUIKit.on(uni.$TUIKitEvent.CONVERSATION_LIST_UPDATED, this.onConversationListUpdated);
		},

		// 下拉刷新
		onPullDownRefresh() {
			// 登入后拉去会话列表
			this.getConversationList();
			uni.$TUIKit.on(uni.$TUIKitEvent.CONVERSATION_LIST_UPDATED, this.onConversationListUpdated);
		},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload() {
			uni.$TUIKit.off(uni.$TUIKitEvent.SDK_READY, this.onConversationListUpdated);
		},

		methods: {

			handleRoute(id) {
				const url = `../../TUI-Chat/chat?conversationID=${id}`;
				uni.navigateTo({
					url
				});
			},
			onConversationListUpdated(event) {
				logger.log('TUI-conversation | onConversationListUpdated  |ok');
				this.setData({
					conversationList: event.data
				});
			},

			getConversationList() {
				uni.showLoading({
					title: '加载中...'
				})
				uni.$TUIKit.getConversationList().then(imResponse => {
					logger.log(
						`TUI-conversation | getConversationList | getConversationList-length: ${imResponse.data.conversationList.length}`
					);
					uni.stopPullDownRefresh()
					uni.hideLoading()
					this.setData({
						conversationList: imResponse.data.conversationList
					});
				});
			},
		}
	};
</script>
<style scoped>
	@import './conversation.css';
</style>
